package com.hc.mybatis.generator.plugins.internal;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.internal.DefaultCommentGenerator;

import java.util.Iterator;
import java.util.List;

/**
 * @Description: 生成数据库表中comments字段
 * @author: hc
 * @version: v1.0
 * @create at: 2020/7/15 20:18
 * @reviewer:
 * @review at:
 *
 * Revision history:
 * date             author      version     content
 * ------------------------------------------------------------
 * 2020/7/15           hc          v1.0        XXXX
 */
public class CustomeCommentGenerator extends DefaultCommentGenerator {

    public CustomeCommentGenerator() {
    }

    public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {
        method.addJavaDocLine("/**");
        String method_name = method.getName();
        if ("selectByExample".equals(method_name)) {
            method.addJavaDocLine(" * 根据条件查询列表");
        } else if (method_name.startsWith("updateByExample")) {
            method.addJavaDocLine(" * 选择性更新数据库记录");
        } else if ("updateByPrimaryKeySelective".equals(method_name)) {
            method.addJavaDocLine(" * 根据主键来更新部分数据库记录");
        } else if ("deleteByPrimaryKey".equals(method_name)) {
            method.addJavaDocLine(" * 根据主键删除数据库的记录");
        } else if ("insert".equals(method_name)) {
            method.addJavaDocLine(" * 插入数据库记录");
        } else if ("selectByPrimaryKey".equals(method_name)) {
            method.addJavaDocLine(" * 根据主键获取一条数据库记录");
        } else if ("updateByPrimaryKey".equals(method_name)) {
            method.addJavaDocLine(" * 根据主键来更新数据库记录");
        } else if ("selectAll".equals(method_name)) {
            method.addJavaDocLine(" * 获取全部数据库记录");
        } else if ("countByExample".equals(method_name)) {
            method.addJavaDocLine(" * 根据条件计数");
        } else if ("insertSelective".equals(method_name)) {
            method.addJavaDocLine(" * 插入数据库记录");
        } else if ("selectOneByExample".equals(method_name)) {
            method.addJavaDocLine(" * 根据条件查询单条记录");
        }

        method.addJavaDocLine(" *");
        List<Parameter> parameterList = method.getParameters();
        Iterator var6 = parameterList.iterator();

        while (var6.hasNext()) {
            Parameter parameter = (Parameter) var6.next();
            String paramterName = parameter.getName();
            method.addJavaDocLine(" * @param " + paramterName);
        }

        method.addJavaDocLine(" */");
    }

    public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
        StringBuffer sb = new StringBuffer();
        field.addJavaDocLine("/**");
        if (introspectedColumn.getRemarks() != null) {
            field.addJavaDocLine(" * " + introspectedColumn.getRemarks());
        }

        sb.append(" * 表 : ");
        sb.append(introspectedTable.getFullyQualifiedTable());
        field.addJavaDocLine(sb.toString());
        field.addJavaDocLine(" * 对应字段 : " + introspectedColumn.getActualColumnName());
        field.addJavaDocLine(" */");
    }

    public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
        new StringBuffer();
        method.addJavaDocLine("/**");
        method.addJavaDocLine(" * get method ");
        method.addJavaDocLine(" *");
        StringBuffer sb = new StringBuffer();
        sb.append(" * @return ");
        sb.append(introspectedTable.getFullyQualifiedTable());
        sb.append('.');
        sb.append(introspectedColumn.getActualColumnName());
        sb.append("：");
        sb.append(introspectedColumn.getRemarks());
        method.addJavaDocLine(sb.toString());
        method.addJavaDocLine(" */");
    }

    public void addSetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
        method.addJavaDocLine("/**");
        method.addJavaDocLine(" * set method ");
        method.addJavaDocLine(" *");
        Parameter parm = (Parameter) method.getParameters().get(0);
        method.addJavaDocLine(" * @param " + parm.getName() + "  " + introspectedColumn.getRemarks());
        method.addJavaDocLine(" */");
    }

    public void addComment(XmlElement xmlElement) {
        xmlElement.addElement(new TextElement("<!--"));
        StringBuilder sb = new StringBuilder();
        sb.append("  @mbggenerated");
        xmlElement.addElement(new TextElement(sb.toString()));
        String s = this.getDateString();
        if (s != null) {
            sb.setLength(0);
            sb.append("  This element is automatically generated by MyBatis Generator,Do not modify ! Generated on ");
            sb.append(s);
            sb.append('.');
            xmlElement.addElement(new TextElement(sb.toString()));
        }

        xmlElement.addElement(new TextElement("-->"));
    }
}

